﻿/*
 * Copyright (C) 2009-2010 Valerio Fuoglio <valerio.fuoglio@gmail.com>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

using System.IO;

namespace LightScrobbler
{
    internal partial class MainDialog
    {

        // Called by DriveDetector when removable device in inserted
        private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
        {
            string drive = e.Drive.Substring(0, e.Drive.Length - 1);
            foreach (object item in _driveCombo.Items)
            {
                if (((string)item).StartsWith(drive))
                    return;
            }

            string label = null;
            foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
            {
                if (!driveInfo.Name.StartsWith(drive))
                    continue;

                label = driveInfo.VolumeLabel;
            }

            if (label != "")
                _driveCombo.Items.Add(drive + " (" + label + ")");
            else
                _driveCombo.Items.Add(drive);

            if (_driveCombo.Items.Count == 1)
                _driveCombo.SelectedIndex = 0;

            _driveRadio.Enabled = true;

            // set HookQueryRemove to true
            e.HookQueryRemove = true;
        }

        // Called by DriveDetector after removable device has been unplugged
        private void OnDriveRemoved(object sender, DriveDetectorEventArgs e)
        {
            string drive = e.Drive.Substring(0, e.Drive.Length - 1);

            if (_selectFileTextBox.Text.StartsWith(drive))
                _selectFileTextBox.Text = "";

            foreach (object item in _driveCombo.Items)
            {
                if (!((string)item).StartsWith(drive))
                    continue;

                _driveCombo.Items.Remove(item);
                break;
            }

            if (_driveCombo.Items.Count == 0)
            {
                _driveRadio.Enabled = false;
                _fileRadio.Checked = true;
                ManageSrcRadio(_fileRadio);
            }
        }

        // Called by DriveDetector when removable drive is about to be removed
        private void OnQueryRemove(object sender, DriveDetectorEventArgs e)
        {
            string drive = e.Drive.Substring(0, e.Drive.Length - 1);
            if (ScrobbleSessionData.GetInstance().DriveLocked &&
                ScrobbleSessionData.GetInstance().FilePath.StartsWith(drive))
            {
                e.Cancel = true;
                return;
            }
            e.Cancel = false;
        }

    }
}